package com.ecar.common.cipher;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * Created by admin on 2016/6/20.
 */
public class DES {

	/**
	 * 生成 DES 的密钥
	 * @return
	 */
	public static String generateKey() {
		String key = null;
		SecureRandom sr = new SecureRandom();
		KeyGenerator kgen;
		try {
			kgen = KeyGenerator.getInstance("DES");
			kgen.init(sr);
			SecretKey skey = kgen.generateKey();
			byte[] keyRawData = skey.getEncoded();
			key = Hex.encodeHexString(keyRawData);
			//			key = bytesToHexString(keyRawData);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return key;
	}

	private static String bytesToHexString(byte[] bytes) {
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (byte b : bytes) {
			int hex = b & 0xff;
			if (hex < 16)
				sb.append('0');
			sb.append(Integer.toHexString(hex));
		}
		return sb.toString().toUpperCase();
	}

	private static byte[] hexToBytes(char[] hex) {
		int length = hex.length / 2;
		byte[] raw = new byte[length];
		for (int i = 0; i < length; i++) {
			int high = Character.digit(hex[i * 2], 16);
			int low = Character.digit(hex[i * 2 + 1], 16);
			int value = (high << 4) | low;
			if (value > 127) value -= 256;
			raw[i] = (byte) value;
		}
		return raw;
	}

	/**
	 * DES 加密
	 * @param key
	 * @param str
	 * @return
	 */
	public static String encrypt(String key, String str) {
		String result = null;
		try {
			//			byte rawData[] = hexToBytes(key.toCharArray());
			byte rawData[] = Hex.decodeHex(key.toCharArray());

			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			Cipher cipher = Cipher.getInstance("DES");

			SecureRandom sr = new SecureRandom();
			DESKeySpec dks = new DESKeySpec(rawData);

			SecretKey skey = keyFactory.generateSecret(dks);
			cipher.init(Cipher.ENCRYPT_MODE, skey, sr);

			byte[] encrypted = cipher.doFinal(str.getBytes());
			result = Hex.encodeHexString(encrypted);
			//			result = bytesToHexString(encrypted);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * DES 解密
	 * @param key
	 * @param str
	 * @return
	 */
	public static String decrypt(String key, String str) {
		String result = null;
		try {
			//			byte[] encrypted = hexToBytes(str.toCharArray());
			//			byte rawData[] = hexToBytes(key.toCharArray());
			byte[] encrypted = Hex.decodeHex(str.toCharArray());
			byte rawData[] = Hex.decodeHex(key.toCharArray());

			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			Cipher cipher = Cipher.getInstance("DES");
			SecureRandom sr = new SecureRandom();
			DESKeySpec dks = new DESKeySpec(rawData);

			SecretKey skey = keyFactory.generateSecret(dks);
			cipher.init(Cipher.DECRYPT_MODE, skey, sr);

			byte[] decrypted = cipher.doFinal(encrypted);
			result = new String(decrypted);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String args[]) {
		String encoded = "";
		String key = "31c149c7c2d69243";
		System.out.println(decrypt(key, encoded));
	}
}
